package com.psct.social;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.psct.social.SocialManager.OnBindListener;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.net.RequestListener;

/**
 * �����ʺŹ�����
 * 
 * @author zjingchuan
 * 
 */
public class SinaAccount extends SocialAccount {
	/**
	 * sina appkey����������õĲ���key
	 */
	public static final String SINA_CONSUMER_KEY = "837157755";
	public static final String SINA_REDIRECT_URL = "http://www.sina.com";

	@Override
	public void bind(final Context context, OnBindListener listener) {
		this.listener = listener;
		Weibo mWeibo = Weibo.getInstance(SINA_CONSUMER_KEY, SINA_REDIRECT_URL);
		mWeibo.authorize(context, new WeiboAuthListener() {
			@Override
			public void onComplete(Bundle values) {
				String token = values.getString("access_token");
				String expires_in = values.getString("expires_in");
				String uid = values.getString("uid");
				Oauth2AccessToken accessToken = new Oauth2AccessToken(token, expires_in);
				if (accessToken.isSessionValid()) {
					String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
							.format(new java.util.Date(accessToken.getExpiresTime()));
					Log.v("oauth", "��֤�ɹ�: \r\n access_token: " + token + "\r\n" + "expires_in: "
							+ expires_in + "\r\n��Ч�ڣ�" + date + "\r\nuid:" + uid);
					// ����token
					SocialAccountKeeper.keepAccessToken(context, accessToken);
					getUserInfo(context, uid, accessToken);
				}
			}

			@Override
			public void onCancel() {

			}

			@Override
			public void onError(WeiboDialogError arg0) {

			}

			@Override
			public void onWeiboException(WeiboException arg0) {

			}

		});
	}

	/**
	 * ��ȡ�û�ͷ����ǳƵ����ϣ�
	 * 
	 * @param context
	 * @param uid
	 * @param accessToken
	 */
	private void getUserInfo(final Context context, String uid, Oauth2AccessToken accessToken) {
		UsersAPI userApi = new UsersAPI(accessToken);
		userApi.show(Long.parseLong(uid), new RequestListener() {

			@Override
			public void onComplete(String result) {
				// TODO Auto-generated method stub
				Log.v("sina", result);
				try {
					JSONObject jsonObject = new JSONObject(result);
					String name = jsonObject.getString("name");
					String imageUrl = jsonObject.getString("profile_image_url");
					try {
						handlUsernfo(context, name, imageUrl, "sina");
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					Log.v("sina", "name:" + name + "\nͷ���ַ��" + imageUrl);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onError(WeiboException arg0) {
				Log.v("sina", "WeiboException");
				arg0.printStackTrace();
			}

			@Override
			public void onIOException(IOException arg0) {
				Log.v("sina", "IOException");
				arg0.printStackTrace();
			}
		});
	}

	@Override
	public void unBind(Context context) {
		SocialAccountKeeper.removeSinaInfo(context);

	}

	@Override
	public void sendMessage(Context context, String content, String picPath,final SendResultListener resultListener) {
		Oauth2AccessToken token = SocialAccountKeeper.getSinaAccessToken(context);
		if (token == null)
			return;
		StatusesAPI api = new StatusesAPI(token);
		if (TextUtils.isEmpty(picPath)) {
			Log.v("social","�������ݣ�"+content);
			api.update(content, "", "", new RequestListener() {

				@Override
				public void onComplete(String arg0) {
					if(resultListener!=null)
					resultListener.onSendOK();
					Log.v("social", "���˷�������onComplete");
				}

				@Override
				public void onError(WeiboException arg0) {
					arg0.printStackTrace();
					if(resultListener!=null)
						resultListener.onSendErr("同步新浪微博失败");
					Log.v("social", "���˷�������onError");
				}

				@Override
				public void onIOException(IOException arg0) {
					arg0.printStackTrace();
					if(resultListener!=null)
						resultListener.onNetErr();
					Log.v("social", "���˷�������onIOException");
				}

			});
		} else {
			Log.v("social", picPath);
			api.upload(content, picPath, "", "", new RequestListener() {

				@Override
				public void onComplete(String arg0) {
					Log.v("social", "���˷���ͼ��onComplete" + arg0);
				}

				@Override
				public void onError(WeiboException arg0) {
					arg0.printStackTrace();
					Log.v("social", "���˷���ͼ��onError");
				}

				@Override
				public void onIOException(IOException arg0) {
					arg0.printStackTrace();
					Log.v("social", "���˷���ͼ��onIOException");
				}

			});
		}

	}

}
